﻿function openModal(url, width, resizable, selector) {
    selector = selector || '#dialog-container';
    var dframe = $(selector).attr('src', url).load(function () {
        $(this).dialog({
            autoOpen: true,
            modal: true,
            width: width,
            resizable: resizable || false,
            zIndex: 100000,
            overlay: { opacity: 0.5, background: 'black' },
            title: dframe.contents().find('title').html()
        });
        $(this).width(width).height($(this).contents().height());
        $('.ui-dialog').animate({ top: $(document).scrollTop() + ($(window).height() - $('.ui-dialog').height()) / 2 }, { duration: 600, queue: false });
    });
};

function closeModal(selector) {
    selector = selector || '#dialog-container';
    $(selector).dialog("close");
};

$(function () {
    iconHover();
    $(window).scroll(function () {
        $('.ui-dialog').animate({ top: $(document).scrollTop() + ($(window).height() - $('.ui-dialog').height()) / 2 }, { duration: 600, queue: false });
    });
    $('.button').button();
    $('input[placeholder]').focus(function () {
        if ($(this).val() == $(this).attr('placeholder'))
            $(this).val('');
    }).blur(function () {
        if ($(this).val() == '')
            $(this).val($(this).attr('placeholder'));
    }).each(function () { $(this).val($(this).attr('placeholder')); });
});

function send(data, method, callback, url) {
    method = method || 'POST';
    url = url || location.href;
    callback = callback || function callback(data) {
        alert(data.message);
        if (data.status)
            location.reload();
    };
    $.ajax({
        type: method,
        url: url,
        data: data,
        dataType: 'JSON',
        success: callback
    });
    return false;
};

function iconHover() {
    $('.icon-button').hover(function () {
        $(this).removeClass('ui-state-default').addClass('ui-state-hover');
    }, function () {
        $(this).removeClass('ui-state-hover').addClass('ui-state-default');
    });
};

function go(href) {
    location.href = href || location.href;
};


/**
* Methods for creating, reading, and deleting cookies.
*/
var Cookie = {

    /**
    * Cached cookies.
    */
    cache: {},

    /**
    * Create a cookie. Can accept a third parameter as a literal object of options.
    *
    * @param key
    * @param value
    * @param options
    */
    create: function (key, value, options) {
        options = $.extend({}, options);
        options.expires = options.expires || 1; // Default expiration: 1 hour

        if (typeof options.expires == 'number') {
            var hours = options.expires;
            options.expires = new Date();
            options.expires.setTime(options.expires.getTime() + (hours * 3600000));
        }

        var cookie = [
			encodeURIComponent(key) + '=',
			options.escape ? encodeURIComponent(value) : value,
			options.expires ? '; expires=' + options.expires.toUTCString() : '',
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		];

        document.cookie = cookie.join('');

        if (Cookie.cache) {
            if (options.expires.getTime() < (new Date()).getTime())
                delete Cookie.cache[key];
            else
                Cookie.cache[key] = value;
        }
    },

    /**
    * Read a cookie.
    *
    * @param key
    * @return string
    */
    read: function (key) {
        // Use cache when available
        if (Cookie.cache[key])
            return Cookie.cache[key];

        var cache = {};
        var cookies = document.cookie.split(';');

        if (cookies.length > 0) {
            for (var i = 0; i < cookies.length; i++) {
                var parts = cookies[i].split('=');

                if (parts.length >= 2)
                    cache[$.trim(parts[0])] = parts[1];
            }
        }

        Cookie.cache = cache;
        return cache[key] || null;
    },

    /**
    * Delete a cookie.
    *
    * @param key
    */
    erase: function (key, options) {
        if (!options) {
            options = { expires: -1 };

        } else if (!options.expires) {
            options.expires = -1;
        }

        Cookie.create(key, 0, options);
    },

    /**
    * Returns whether cookies are supported/enabled by the browser
    *
    * @return boolean
    */
    isSupported: function () {
        return (document.cookie.indexOf('=') != -1);
    }
};
$(document).ready(function () {
    $(window).scroll(function () {
        $('.ui-dialog').animate({ top: $(document).scrollTop() + ($(window).height() - $('.ui-dialog').height()) / 2 }, { duration: 600, queue: false });
    });
});

function show(url, width) {
    var dframe = $('#dialog-container').attr('src', url);
    dframe.load(function () {
        $(this).dialog({
            autoOpen: true,
            modal: true,
            width: width,
            zIndex: 100001,
            overlay: { opacity: 0.5, background: 'black' },
            title: dframe.contents().find('title').html()
        });
        $(this).width(width).height($(this).contents().height());
        $('.ui-dialog').animate({ top: $(document).scrollTop() + ($(window).height() - $('.ui-dialog').height()) / 2 }, { duration: 600, queue: false });
    });
}

function closeModal() {
    $("#dialog-container").dialog("close");
}
window.addFavorite = function (url, title) {
    if (document.all)
        window.external.AddFavorite(url, title)
    else if (window.sidebar)
        window.sidebar.addPanel(title, url, "");
};
window.addHomePage = function (url) {
    url = url || location.href;
    if ($.browser.msie) {
        document.body.style.behavior = 'url(#default#homepage)';
        document.body.setHomePage(url);
    } else if (window.sidebar) {
        if (window.netscape) {
            try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) { alert(resources.homepage.disenabled); }
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage', url);
    }
};